import { defineStore } from "pinia";
import Cookies from "js-cookie";

const cookiesStorage: any = {
  setItem(key: string, state: any) {
    return Cookies.set(key, state.accessToken, { expires: 3 });
  },
  getItem(key: string) {
    return JSON.stringify({ accessToken: Cookies.get(key) });
  },
};

let storeHome = defineStore("home", {
  persist: {
    enabled: true,
    strategies: [
      {
        storage: sessionStorage,
        paths: ["age",'name'],
      },
      {
        key: "practiveToken", //如果使用cookie存储，必须加上key，否则value值是undefined
        storage: cookiesStorage,
        paths: ["practiveToken"],
      },
    ],
  },

  state: () => {
    return {
      age: 19,
      name: "sd",
      practiveToken: "practiveToken666",
    };
  },
  getters: {},
  actions: {
    plus() {
      this.age++;
    },
  },
});
export const getToken = () => {
  return storeHome().practiveToken;
};
export default storeHome;
